package utils;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class ImageCutUtil {

    public static void main(String[] args) {
        String formatName = "jpg";
        String iconName = "myicon.jpg";
        String tempPath = "C:/temp";
        String savePath = "C:/save";
        try {
            cutImage(74,108,299,168,formatName,iconName,tempPath,savePath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * @param x 
     * @param y
     * @param w
     * @param h
     * @param formatName 文件格式
     * @param iconName 头像名称
     * @param tempPath 缓存图片路径
     * @param savePath 保存路径
     * @throws IOException 
     */
    public static void cutImage(int x, int y, int w, int h, String formatName, String iconName, String tempPath, String savePath) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(new File(tempPath + File.separator + iconName));
            os = new FileOutputStream(new File(savePath + File.separator + iconName));
            doCutImage(is,os,x,y,w,h,formatName);
        }finally{
        	if(is != null){
        		is.close();
        	}
        	if(os != null){
        		os.close();
        	}
        }
        
    }
    
    private static void doCutImage(InputStream input, OutputStream out, int x,  
            int y, int width, int height, String formatName) throws IOException {  
        ImageInputStream imageStream = null;  
        try {  
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(formatName);  
            ImageReader reader = readers.next();  
            imageStream = ImageIO.createImageInputStream(input);  
            reader.setInput(imageStream, true);  
            ImageReadParam param = reader.getDefaultReadParam();  
              
            System.out.println(reader.getWidth(0));  
            System.out.println(reader.getHeight(0));  
              
            Rectangle rect = new Rectangle(x, y, width, height);  
            param.setSourceRegion(rect);  
            BufferedImage bi = reader.read(0, param);  
            ImageIO.write(bi, "png", out);  
        } finally {  
            imageStream.close();  
        }  
    }  
    
    /**
     * 压缩图片算法
     * @param is 代表图片的输入流
     * @param os 图片文件保存用的输出流
     * @param formatName 文件格式
     * @throws IOException
     */
    public static void compressImage(InputStream is, OutputStream os, String formatName) throws IOException { 
        try {
            BufferedImage image = ImageIO.read(is);
            float width = 800;
            float height = 640;
            // 获得缩放的比例
            double ratio = 1.0;
            // 判断如果高、宽都不大于设定值，则不处理
            if (image.getHeight() > height || image.getWidth() > width) {
                if (image.getHeight() > image.getWidth()) {
                    ratio = height / image.getHeight();
                } else {
                    ratio = width / image.getWidth();
                }
            }
            // 计算新的图面宽度和高度
            int newWidth = (int) (image.getWidth() * ratio);
            int newHeight = (int) (image.getHeight() * ratio);

            BufferedImage bfImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            bfImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
            ImageIO.write(bfImage, formatName, os);  
        } finally {
            is.close();  
            os.close();  
        }  
    }
}
